<template>
  <div class="home">
    <div>
      <input type="text" v-model="news.title" placeholder="录入新闻标题"><br>
      <input type="text" v-model="news.content" placeholder="录入新闻内容"><br>
       <input type="text" v-model="news.author" placeholder="录入新闻作者"><br>
       <button @click="addNews()">增加</button>
    </div>
   <table border="1">
     <thead>
       <th>标题</th>
       <th>内容</th>
       <th>作者</th>
     </thead>
     <tr v-for="(n,index) in list" :key="index" >
        <td width="20%">{{n.id}}</td> 
       <td width="20%">{{n.title}}</td>
       <td width="60%">{{n.content}}</td>
       <td width="10%">{{n.author}}</td>
     </tr>
   </table>
  </div>
</template>

<script>
import sha1 from "sha1";
export default {
data() {
  return {
    list: [],
    news:{}
  }
},
mounted () {
  this.getApiList();
},
  methods: {
    getApiList() {
      var app_id='A6106858749926';
      var now = Date.now();
      var appKey = sha1(app_id+"UZ"+"695152D9-58A7-E8FF-B006-2B7678543AFC"+"UZ"+now)+"."+now
      let url="https://d.app3c.cn/mcm/api/News/"
      this.axios({
        "method":"get",
        "url":url,
       "headers": {
        "X-APICloud-AppId": app_id,
        "X-APICloud-AppKey":appKey
      }
      }).then(res=>{
        console.log(res)
        this.list=res.data;
      })
    },
    addNews()
    {
      var app_id='A6106858749926';
      var now = Date.now();
      var appKey = sha1(app_id+"UZ"+"695152D9-58A7-E8FF-B006-2B7678543AFC"+"UZ"+now)+"."+now
      let url="https://d.app3c.cn/mcm/api/News/"
      this.axios({
        "method":"post",
        "url":url,
       "headers": {
        "X-APICloud-AppId": app_id,
        "X-APICloud-AppKey":appKey
      },
      "data":{
        title:this.news.title,
        content:this.news.content,
        author:this.news.author
      }
      }).then(res=>{
        if(res.status==200){
          alert("插入成功");
          this.getApiList();
        }      
        else
          alert("插入失败");
      })
    }
  },

}
</script>
